package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Product;
import ysq.xyg.model.ProductType;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ProductInfo;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ProductService;
import ysq.xyg.service.ProductTypeService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(ProductController.class);
    @Resource
    private ProductService productService;
    @Resource
    private ProductTypeService productTypeService;

    private Product product;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/productList");
    }
    @RequestMapping("/listPageK")
    public ModelAndView ListK(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/productListK");
    }

    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(Product product,Page page) throws MalformedURLException, UnsupportedEncodingException {
        product.setProductTypeId("2");
        PageInfo<Product> pageInfo=productService.pageInfoLeft(product,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(Product item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("name",item.getName());
                row.put("deviceId",item.getDeviceId());
                row.put("productTypeId",item.getProductTypeId());
                row.put("openid",item.getOpenid());
                row.put("nickName",item.getNickName());
                row.put("phone",item.getPhone());
                if(item.getDeviceDate()!=null){
                    row.put("deviceDate",DateUtil.DateToStr(item.getDeviceDate(), DateUtil.DATE_TIME_PATTERN));
                }else{
                    row.put("deviceDate","未设置");
                }
                row.put("deviceCity",item.getDeviceCity());
                row.put("deviceBat",item.getDeviceBat());
                row.put("productTypeName",item.getProductTypeName());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }
    @RequestMapping(value = "/JsonListK",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonListK(Product product,Page page) throws MalformedURLException, UnsupportedEncodingException {
        product.setProductTypeId("1");
        PageInfo<Product> pageInfo=productService.pageInfoLeftK(product,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(Product item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("name",item.getName());
                row.put("deviceId",item.getDeviceId());
                row.put("productTypeId",item.getProductTypeId());
                row.put("openid",item.getOpenid());
                row.put("nickName",item.getNickName());
                row.put("phone",item.getPhone());
                if(item.getDeviceDate()!=null){
                    row.put("deviceDate",DateUtil.DateToStr(item.getDeviceDate(), DateUtil.DATE_TIME_PATTERN));
                }else{
                    row.put("deviceDate","未设置");
                }
                row.put("relayAuto",item.getRelayAuto());
                row.put("relayKm",item.getRelayKm());
                row.put("relayPower",item.getRelayPower());
                row.put("relayStatus",item.getRelayStatus());
                row.put("productTypeName",item.getProductTypeName());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }
    @RequestMapping(value = "/SelectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id){
        ModelAndView view=new ModelAndView("/productUpdateOrSave");
        product=productService.selectByPrimaryKey(id);
        view.addObject("product",product);
        return view;
    }


}
